package com.zhl.methods_reference6;

import com.zhl.arrays4.Student;

import java.util.Arrays;
import java.util.Comparator;

public class Test1 {
        public static void main(String[] args) {
            Student[] students = new Student[5];
            students[0] = new Student(20,176.5, "柳岩");
            students[1] = new Student(25,180.5, "大鹏");
            students[2] = new Student(22,178.5, "章若楠");
            students[3] = new Student(43,182.5, "刘亦菲");
            students[4] = new Student(74,179.5, "沈腾");

            //原始写法，对数组中的学生对象，按照年龄进行升序排序

            Arrays.sort(students, new Comparator<Student>() {
                @Override
                public int compare(Student o1, Student o2) {
                    return Double.compare(o1.getHeight(), o2.getHeight());//升序  ,如果想降序,调换以下位置就行。

                }
            });

//            //使用lambda进行简化
//            Arrays.sort(students,(o1,o2) -> o1.getAge()-o2.getAge());
//            //进一步简化  如果某个Lambda表达式里只是调用一个静态方法,并且前后参数的形式一致,就可以使用静态方法引用。
//            //类名::静态方法名
//            Arrays.sort(students, CompareByData::compare);

            //降序
            Arrays.sort(students, (o1, o2) -> o2.getAge() - o1.getAge());
            //简化
            CompareByData cbd = new CompareByData();
            Arrays.sort(students, (o1, o2) ->cbd.compareByAge(o1, o2));
            //进一步简化   实例方法引用
            Arrays.sort(students, cbd::compareByAge);

            System.out.println("排序后："+Arrays.toString(students));
        }


}
